package net.yxsoft.kit;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.ehcache.CacheKit;
import net.yxsoft.model.infrastructure.TsUser;
import net.yxsoft.resource.CacheRes;
import net.yxsoft.resource.Res;
import rapid.util.StrKit;

import java.util.HashMap;
import java.util.Map;

public class AccessTokenKit {
    public static Map<String,String>  getAccessToken(String code){
        String accessTokenurl = PropKit.get("ysf.accessTokenurl");
        String backendToken = BackendTokenKit.getBackendToken();
        String appid = PropKit.get("ysf.appId");

        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("appId", appid);
        paramMap.put("backendToken", backendToken);
        paramMap.put("code", code);
        paramMap.put("grantType", Res.grantType);
        String jsonStr = HttpKit.post(accessTokenurl, JsonKit.toJson(paramMap));
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        Map<String,String> map= new HashMap<String,String>();

        if(jsonObject.getString("resp").equals("00")) {
            JSONObject jsonObjectparames = JSONObject.parseObject(jsonObject.getString("params"));
            map.put("accessToken", jsonObjectparames.getString("accessToken"));
            map.put("openId", jsonObjectparames.getString("openId"));
            map.put("unionId", jsonObjectparames.getString("unionId"));
        }
        return map;

    }
}
